package com.eusoft.demo_project.controller;


import com.eusoft.demo_project.entity.*;
import com.eusoft.demo_project.service.CarManageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/carManage")
public class CarManageController {

    @Resource
    CarManageService carManageService;

    @GetMapping("/carTypeList")
    public String carTypeList(Model model){
        List<Car_type> car_types = carManageService.queryCarTypeList();
        model.addAttribute("code",10000);
        model.addAttribute("message","成功");
        model.addAttribute("data",car_types);
        return "";
    }

    @GetMapping("/carStateList")
    public String carStateList(Model model){
        List<Car_state> car_states = carManageService.queryCarStateList();
        model.addAttribute("code",10000);
        model.addAttribute("message","成功");
        model.addAttribute("data",car_states);
        return "";
    }


    @GetMapping("/carList")
    public String carList(@RequestParam(required = false)Integer id,
                          @RequestParam(required = false)Integer stateId,
                          @RequestParam(required = false)String search,
                          @RequestParam(defaultValue = "1")Integer page,
                          @RequestParam(defaultValue = "10")Integer pageRows,
                          Model model){
        Integer offset = (page - 1)* pageRows;
        DataType<Car> data = new DataType<>();
        data.setPage(page);
        data.setPageNum(pageRows);
        Integer count = carManageService.countCarList(id, stateId, search);
        data.setRowsNum(count);
        data.setList(carManageService.queryCarList(id,stateId,search,offset,pageRows));
        model.addAttribute("code",10000)
                .addAttribute("message","成功")
                .addAttribute("data",data);

        Integer index = count/10 +1;
        List indexs = new ArrayList();
        for (int i = 1;i<=index;i++ )
            indexs.add(i);

        model.addAttribute("indexs",indexs);
        model.addAttribute("aid",id).addAttribute("stateId",stateId).addAttribute("search",search);
        model.addAttribute("carType",carManageService.queryCarTypeList())
                .addAttribute("carState",carManageService.queryCarStateList());
        return "carManage/list";
    }

    @PostMapping("/delCar")
    public String delCar(@RequestParam(required = true)Integer aid,Model model){
        if (aid != null){
            //删除车辆信息 car_user、car
            try {
                carManageService.delCar(aid);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                model.addAttribute("code",10000)
                        .addAttribute("message","成功")
                        .addAttribute("data",null);
                return "redirect:/carManage/carList";
            }

        }
        else {
            model.addAttribute("code",500);
            model.addAttribute("message","请求参数aid为空");
            System.out.println("请求参数aid为空");
            return "";
        }

    }

    @GetMapping("/carDetail")
    public String carDetail(@RequestParam(required = false) Integer aid,
                            @RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "10")Integer pageRows,Model model){

        if(aid != null){
            model.addAttribute("code",10000)
                    .addAttribute("message","成功")
                    .addAttribute("data",carManageService.queryCarDetail(aid));
        }
        else {
            model.addAttribute("code",500)
                    .addAttribute("message","请求参数aid为空")
                    .addAttribute("data",null);
        }

        Integer offset = (page - 1)* pageRows;
        model.addAttribute("carUser",carManageService.querycarUserList(aid,offset,pageRows));
        return "carManage/carDetail";

    }

    @GetMapping("/carUserList")
    public String carUserList(@RequestParam(required = false)Integer aid,
                              @RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10")Integer pageRows,
                              Model model){
        if (aid != null){
            Integer offset = (page - 1)* pageRows;
            DataType<User> data = new DataType<>();
            data.setPage(page);
            data.setPageNum(pageRows);
            data.setRowsNum(carManageService.countCarUser(aid));
            data.setList(carManageService.querycarUserList(aid,offset,pageRows));

            model.addAttribute("code",10000)
                    .addAttribute("message","成功")
                    .addAttribute("data",data);
        }
        else{
            model.addAttribute("code",500)
                    .addAttribute("message","请求参数aid为空")
                    .addAttribute("data",null);
        }


        return "";
    }

    @GetMapping("/carInfo")
    public String carInfo(@RequestParam Integer aid, Model model){
//        if(aid != null){
            model.addAttribute("code",10000)
                    .addAttribute("message","成功")
                    .addAttribute("data",carManageService.queryCarInfo(aid))
                    .addAttribute("carType",carManageService.queryCarTypeList())
                    .addAttribute("carState",carManageService.queryCarStateList())
                    .addAttribute("users",carManageService.queryAllUser());
//        }
//        else{
//            model.addAttribute("code",500)
//                    .addAttribute("message","请求参数aid为空")
//                    .addAttribute("data",null);
//        }
        return "carManage/carInfo";
    }

    @GetMapping("/checkCarPlateCode")
    public String checkCarPlateCode(@RequestParam Integer aid,
                                    @RequestParam String plateCode,
                                    Model model){
        Map<String,Boolean> data = new HashMap<>();
        data.put("isExists",carManageService.checkCarPlateCode(plateCode,aid));
        model.addAttribute("code",10000)
                .addAttribute("message","成功")
                .addAttribute("data",data);
        return "";
    }

    @PostMapping("/saveCar")
    public String saveCar(@RequestParam Integer aid,
                          @RequestParam String plateCode,
                          @RequestParam Integer typeId,
                          @RequestParam String type_name,
                          @RequestParam String brand,
                          @RequestParam Integer approvedNum,
                          @RequestParam String  memo,
                          @RequestParam Integer stateId,
                          @RequestParam String stateName,
                          @RequestParam List user_ids,
                          Model model){
        carManageService.addOrUpdateCar(aid,user_ids,plateCode,brand,typeId,type_name,stateId,stateName,approvedNum,memo);
        model.addAttribute("code",10000).addAttribute("message","成功");
        return "carManage/list";
    }
}
